package com.party.mobile.biz.order.payOrder;

import com.party.common.constant.Constant;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.YesNoStatus;
import com.party.core.model.activity.ActStatus;
import com.party.core.model.activity.Activity;
import com.party.core.model.copartner.BrokerageItem;
import com.party.core.model.crowdfund.Analyze;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.crowdfund.Support;
import com.party.core.model.crowdfund.TargetProject;
import com.party.core.model.distributor.DistributorRelation;
import com.party.core.model.distributor.DistributorRelationType;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberAct;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.PartnerMember;
import com.party.core.model.message.IdentityType;
import com.party.core.model.order.OrderForm;
import com.party.core.model.order.OrderStatus;
import com.party.core.model.order.PaymentState;
import com.party.core.model.order.PaymentWay;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.crowdfund.IAnalyzeService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.crowdfund.ISupportService;
import com.party.core.service.crowdfund.ITargetProjectService;
import com.party.core.service.crowdfund.biz.CrowdfundReviseBizService;
import com.party.core.service.crowdfund.biz.ProjectDetailCountBizService;
import com.party.core.service.member.IMemberActService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.member.IPartnerMemberService;
import com.party.core.service.member.biz.MemberPersonalCountBizService;
import com.party.core.service.order.IOrderFormService;
import com.party.mobile.biz.analyze.MemberCopartnerDetailBizService;
import com.party.mobile.biz.analyze.MemberMerchantDetailBizService;
import com.party.mobile.biz.copartner.BrokerageBizService;
import com.party.mobile.biz.crowdfund.ProjectBizService;
import com.party.mobile.biz.crowdfund.SupportBizService;
import com.party.mobile.biz.distributor.DistributorBizService;
import com.party.mobile.biz.order.MessageOrderBizService;
import com.party.mobile.web.dto.crowdfund.input.SupportStatisticsInput;
import com.party.notify.notifyPush.officialPartner.ICopartnerNotifySendService;
import com.party.notify.notifyPush.officialPartner.IOfficialPartnerNotifySendService;
import com.party.notify.notifyPush.servce.INotifySendService;
import com.party.pay.service.pay.PayOrderBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName CrowdfundPayOrderService
 * @Description 单场众筹订单回调处理
 * @Author wei.li
 * @Date 2019/4/17
 **/
@Service
@Component("crowdfundPayOrderService")
public class CrowdfundPayOrderService extends IPayOrderService {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ISupportService supportService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private ITargetProjectService targetProjectService;
    @Autowired
    private IActivityService activityService;
    @Autowired
    private ProjectBizService projectBizService;
    @Autowired
    private MessageOrderBizService messageOrderBizService;
    @Autowired
    private CrowdfundReviseBizService crowdfundReviseBizService;
    @Autowired
    private IAnalyzeService analyzeService;
    @Autowired
    private IOrderFormService orderFormService;
    @Autowired
    private IMemberActService memberActService;
    @Autowired
    private IPartnerMemberService partnerMemberService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IMemberGroupService memberGroupService;
    @Autowired
    private MemberPersonalCountBizService memberPersonalCountBizService;
    @Autowired
    private ProjectDetailCountBizService projectDetailCountBizService;
    @Autowired
    private PayOrderBizService payOrderBizService;
    @Autowired
    private INotifySendService notifySendService;
    @Autowired
    private DistributorBizService distributorBizService;
    @Autowired
    private ICopartnerNotifySendService copartnerNotifySendService;
    @Autowired
    private IOfficialPartnerNotifySendService officialPartnerNotifySendService;
    @Autowired
    private MemberMerchantDetailBizService memberMerchantDetailBizService;
    @Autowired
    private BrokerageBizService brokerageBizService;
    @Autowired
    private SupportBizService supportBizService;
    @Autowired
    private MemberCopartnerDetailBizService memberCopartnerDetailBizService;

    @Override
    public Object doMain(OrderForm orderForm) {
        // 修改支持状态
        Support support = supportService.findByOrderId(orderForm.getId());
        support.setPayStatus(YesNoStatus.YES.getCode());
        supportService.update(support);

        Project project = projectService.get(support.getProjectId());
        Integer isSuccess = null == project.getIsSuccess() ? YesNoStatus.NO.getCode() : project.getIsSuccess();
        TargetProject targetProject = targetProjectService.findByProjectId(project.getId());

        // 更新众筹信息
        this.updateProject(project, orderForm.getPayment());
        // 更新活动信息
        this.updateActivity(orderForm.getPayment(), project, targetProject.getTargetId(), isSuccess);

        // 处理众筹主订单数据
        if (Constant.IS_SUCCESS.equals(project.getIsSuccess()) && !YesNoStatus.YES.getCode().equals(isSuccess)) {
            this.doCrowdfundSuccess(targetProject.getOrderId());
        }
        return isSuccess;
    }

    @Override
    public void doAssist(OrderForm orderForm, Object resultObject) {
        Support support = supportService.findByOrderId(orderForm.getId());
        Project project = projectService.get(support.getProjectId());
        TargetProject targetProject = targetProjectService.findByProjectId(project.getId());
        Activity activity = activityService.get(targetProject.getTargetId());

        // 记录更新前是否已经成功，如果更新前已经成功，就当做没有成功
        // 如果resultObject不是1，project.getIsSuccess()是1，isSuccess为true
        // 如果resultObject是1，project.getIsSuccess()不是1，isSuccess为false
        // 如果resultObject是1，project.getIsSuccess()是1，isSuccess为false
        boolean isSuccess = false;
        Integer success = null;
        if (null != resultObject) {
            success = (Integer) resultObject;
        }

        if (null != success && YesNoStatus.YES.getCode().equals(success)) {
            if (YesNoStatus.YES.getCode().equals(project.getIsSuccess())) {
                isSuccess = false;
            } else if (!YesNoStatus.YES.getCode().equals(project.getIsSuccess())) {
                isSuccess = false;
            }
        } else if (YesNoStatus.YES.getCode().equals(project.getIsSuccess())) {
            isSuccess = true;
        }


        logger.info("projectId：{}，resultObject：{}", project.getId(), resultObject);

        // 校正众筹数据，项目数据
/*        try {
            crowdfundReviseBizService.reviseProjectData(project);
            crowdfundReviseBizService.reviseTargetData(activity);
        } catch (Exception e) {
            logger.error("支持完成后, 立即校验众筹和活动数据异常", e);
        }*/

        // 众筹者
        Member crowdfundMember = memberService.get(project.getAuthorId());

        // 支持者
        Member supportMember = memberService.get(support.getFavorerId());

        // 项目发布者
        MemberGroup orgMember = memberGroupService.get(activity.getMember());

        // 处理众筹排行，阶段提醒消息推送
        long start = System.currentTimeMillis();
        this.doCrowdfundRank(activity, support, project, crowdfundMember, orgMember, orderForm.getPayment());
        long end = System.currentTimeMillis();
        logger.info("处理众筹排行，阶段提醒消息推送：{}(ms)",(end - start));

        // 支持者消息推送，海报推送
        new Thread(() -> {
            try {
                this.supportMessageSend(project, orderForm, activity, support, supportMember, crowdfundMember, orgMember);
            } catch (Exception e) {
                logger.error("支持者消息推送，海报推送", e);
            }
        }).start();

        if (isSuccess) {
            // 成功后的消息推送，合作商数据统计
            long start2 = System.currentTimeMillis();
            this.doSuccessOpt(orderForm, crowdfundMember, orgMember, project, activity, targetProject.getOrderId(), isSuccess);
            long end2 = System.currentTimeMillis();
            logger.info("成功后的消息推送，合作商数据统计，海报推送：{}(ms)",(end2 - start2));
        } else {
            // 合作商数据统计，合伙人数据统计
            this.doNotSuccessOpt(activity, orderForm, isSuccess);
        }
        // 更新数据分析的最近7天的众筹金额
        long start3 = System.currentTimeMillis();
        this.doSevenDayNum(project.getId());
        long end3 = System.currentTimeMillis();
        logger.info("更新数据分析的最近7天的众筹金额：{}(ms)",(end3 - start3));

        // 处理分销的数据
        long start4 = System.currentTimeMillis();
        this.doStatistics(project.getId(), orderForm, isSuccess, activity, crowdfundMember);
        long end4 = System.currentTimeMillis();
        logger.info("处理分销的数据：{}(ms)",(end4 - start4));
        // 更新支持者、众筹者的个人数据
        this.updatePersonalData(activity.getMember(), support.getFavorerId(), project.getAuthorId());
        // 统计众筹某天的数据
        long start5 = System.currentTimeMillis();
        this.updateProjectDetail(project.getId(), support.getCreateDate());
        long end5 = System.currentTimeMillis();
        logger.info("统计众筹某天的数据：{}(ms)",(end5 - start5));
    }

    /**
     * @param projectId 众筹id
     * @return
     * @Author yifeng
     * @Description 更新数据分析的最近7天的众筹金额
     * @Date 11:32 2019/4/11
     **/
    public void doSevenDayNum(String projectId) {
        /*new Thread(() -> {*/
        try {
            Date now = new Date();
            Date startDate = DateUtils.addDay(now, -6);
            // 最近七天的众筹金额
            Float sevenDaysNum = supportService.countBySupportDate(DateUtils.formatDate(startDate),
                    DateUtils.formatDate(now), projectId);
            Analyze analyze = analyzeService.findByTargetId(projectId);
            if (null != analyze) {
                analyze.setSevenDaysNum(sevenDaysNum);
                analyzeService.update(analyze);
                logger.info("支持者支付成功后更新analyze中的sevenDaysNum字段成功");
            }
        } catch (Exception e) {
            logger.error("支持完成后, 更新analyze数据异常", e);
        }
        /*}).start();*/
    }

    /**
     * @param project   众筹
     * @param orderForm 订单
     * @param activity  活动
     * @param support   支持
     * @return
     * @Author yifeng
     * @Description 支持者消息推送
     * @Date 11:12 2019/4/11
     **/
    public void supportMessageSend(Project project, OrderForm orderForm, Activity activity, Support support,
                                   Member supportMember, Member crowdfundMember, MemberGroup orgMember) {
        /*new Thread(() -> {*/
        // 付款类型：0为普通众筹，1为直接付款众筹',
        if (project.getPayType() == 0) {
            final String activityMemberId = activity.getMember();
            try {
                messageOrderBizService.supportWechatSend(orderForm, project, supportMember, crowdfundMember, orgMember);
            } catch (Exception e) {
                logger.error("支持消息推送异常：projectId-" + project.getId() + "，supportId-" + support.getId(), e);
            }

            // 在微信公众号上给支持者推送支持海报
            final String projectId = project.getId();
            final String orderMemberId = orderForm.getMemberId();
            try {
                logger.info("开始推送支持海报");
                long start1 = System.currentTimeMillis();
                messageOrderBizService.supportersPostersSend(projectId, orderMemberId, activityMemberId,
                        String.valueOf(orderForm.getPayment()), crowdfundMember, orgMember, activity, supportMember);
                long end1 = System.currentTimeMillis();
                logger.info("supportersPostersSend 支持者海报生成和推送用时：{}", (end1 - start1));
                logger.info("支持海报推送完成");
            } catch (Exception e) {
                logger.error("推送支持者海报失败", e);
            }
        }
        /*}).start();*/
    }

    /**
     * @param orgId             机构id
     * @param supportMemberId   支持者id
     * @param crowdfundMemberId 众筹者id
     * @return
     * @Author yifeng
     * @Description 处理个人统计数校正
     * @Date 14:30 2019/4/11
     **/
    public void updatePersonalData(String orgId, String supportMemberId, String crowdfundMemberId) {
//        new Thread(() -> {
            try {
                //机构用户
                PartnerMember partnerMember = new PartnerMember();
                partnerMember.setPartnerId(orgId);
                partnerMember.setMemberId(supportMemberId);
                partnerMemberService.getSet(partnerMember);

//                memberPersonalCountBizService.countOne(partnerMember);
            } catch (Exception e) {
                logger.error("处理支持者的个人统计数校正异常", e);
            }
//            try {
//                PartnerMember projectPartnerMemberCount = partnerMemberService.findByPartnerAndMember(orgId, crowdfundMemberId);
//                memberPersonalCountBizService.countOne(projectPartnerMemberCount);
//            } catch (Exception e) {
//                logger.error("处理众筹者的个人统计数校正异常", e);
//            }
//        }).start();
    }

    /**
     * @param projectId         众筹id
     * @param supportCreateDate 创建时间
     * @return
     * @Author yifeng
     * @Description 统计众筹某天的数据
     * @Date 16:01 2019/4/11
     **/
    public void updateProjectDetail(String projectId, Date supportCreateDate) {
        try {
            projectDetailCountBizService.countProjectSomeDay(projectId, supportCreateDate);
        } catch (Exception e) {
            logger.error("众筹统计分析数据更新异常", e);
        }
    }

    /**
     * @param activity        项目
     * @param support         支持
     * @param project         众筹
     * @param orgMember       发布者
     * @param crowdfundMember 众筹者
     * @return
     * @Author yifeng
     * @Description 处理众筹排行，阶段提醒消息推送
     * @Date 10:19 2019/4/11
     **/
    public void doCrowdfundRank(Activity activity, Support support, Project project,
                                Member crowdfundMember, MemberGroup orgMember, Float payment) {
        /*new Thread(() -> {*/
        float beforeActual = BigDecimalUtils.sub(project.getActualAmount(), payment);
        beforeActual = BigDecimalUtils.round(beforeActual, 2);
        // 更新前百分比
        int beforePercent = getPercent(beforeActual, project.getTargetAmount());
        // 更新后百分比
        int afterPercent = getPercent(project.getActualAmount(), project.getTargetAmount());
        try {
            projectBizService.doCrowdRank(afterPercent, beforePercent, activity, support, project);
        } catch (Exception e) {
            logger.error("保存众筹排名异常", e);
        }
        try {
            messageOrderBizService.projectGradationSend(project, beforePercent, afterPercent, activity, crowdfundMember, orgMember);
        } catch (Exception e) {
            logger.error("推送阶段提醒消息异常", e);
        }
        /*}).start();*/
    }

    /**
     * @param project 众筹信息
     * @return
     * @Author yifeng
     * @Description 更新众筹信息
     * @Date 9:48 2019/4/11
     **/
    private void updateProject(Project project, Float payment) {
        if (null != project.getRefundState() && !YesNoStatus.YES.getCode().equals(project.getRefundState())) {
            project.setRefundState(YesNoStatus.YES.getCode());
        }
        if(null == project.getRealTimeAmount()){
            project.setRealTimeAmount(0.0f);
        }
        logger.info("projectId：{}，订单金额：{}", project.getId(), payment);
        logger.info("projectId：{}，更新前金额：{}", project.getId(), project.getRealTimeAmount());
        float realTimeAmount = BigDecimalUtils.add(project.getRealTimeAmount(), payment);
        realTimeAmount = BigDecimalUtils.round(realTimeAmount, 2);
        logger.info("projectId：{}，更新后金额：{}", project.getId(), realTimeAmount);

        // 实时已筹集金额
        project.setRealTimeAmount(realTimeAmount);
        // 已筹金额是否大于目标金额
        float actualAmount = realTimeAmount > project.getTargetAmount() ? project.getTargetAmount() : realTimeAmount;
        project.setActualAmount(actualAmount);
        logger.info("projectId：{}，更新前支付人数：{}", project.getId(), project.getFavorerNum());
        project.setFavorerNum(project.getFavorerNum() + 1);
        logger.info("projectId：{}，更新后支付人数：{}", project.getId(), project.getFavorerNum());
        // 如果众筹成功
        boolean isSueccess = projectService.isSuccess(project.getTargetAmount(), project.getActualAmount());
        if (!project.getIsSuccess().equals(Constant.IS_SUCCESS) && isSueccess) {
            project.setIsSuccess(Constant.IS_SUCCESS);
        }

        //最后支持时间
        project.setLastSupportDate(DateUtils.getTodayDate());

        // 处理众筹用时
        long now = DateUtils.getTodayDate().getTime();
        long createDate = project.getCreateDate().getTime();
        if (YesNoStatus.NO.getCode().equals(project.getIsSuccess())) {
            project.setUseTime(now - createDate);
        } else if (YesNoStatus.YES.getCode().equals(project.getIsSuccess())) {
            if (null != project.getLastSupportDate()) {
                project.setUseTime(project.getLastSupportDate().getTime() - createDate);
            }
        }
        projectService.update(project);
    }

    /**
     * @param mainOrderId 主订单id
     * @return
     * @Author yifeng
     * @Description 更新众筹主订单的相关数据
     * @Date 14:25 2019/4/11
     **/
    public void doCrowdfundSuccess(String mainOrderId) {
        OrderForm mainOrderForm = orderFormService.get(mainOrderId);
        MemberAct mainMemberAct = memberActService.findByOrderId(mainOrderId);
        mainOrderForm.setIsPay(PaymentState.IS_PAY.getCode());
        mainOrderForm.setPaymentWay(PaymentWay.CROWD_FUND_PAY.getCode());
        mainOrderForm.setStatus(OrderStatus.ORDER_STATUS_HAVE_PAID.getCode());
        mainOrderForm.setUpdateDate(new Date());
        orderFormService.update(mainOrderForm);

        if (mainMemberAct != null) {
            mainMemberAct.setCheckStatus(ActStatus.ACT_STATUS_PAID.getCode());
            mainMemberAct.setPayStatus(MemberAct.PAY_STATUS_YES);
            memberActService.update(mainMemberAct);
        }
    }

    /**
     * @param payment    金额
     * @param project    众筹信息
     * @param activityId 活动id
     * @return
     * @Author yifeng
     * @Description 更新活动信息
     * @Date 9:50 2019/4/11
     **/
    private Activity updateActivity(Float payment, Project project, String activityId, Integer isSuccess) {
        //更改项目
        Activity activity = activityService.get(activityId);
        activity.setFavorerNum(activity.getFavorerNum() + 1);
        activity.setOrderNum(activity.getOrderNum() + 1);

        // 累计资金数
        Double totalMoney = BigDecimalUtils.add(activity.getTotalMoney(), payment);
        totalMoney = BigDecimalUtils.round(totalMoney, 2);
        activity.setTotalMoney(totalMoney);

        if (Constant.IS_SUCCESS.equals(project.getIsSuccess()) && !YesNoStatus.YES.getCode().equals(isSuccess)) {
            // 商品正在众筹人数减
            activity.setCrowdfundedNum(activity.getCrowdfundedNum() + 1);
            Integer beCrowdfundNum = activity.getBeCrowdfundNum() - 1 <= 0 ? 0 : activity.getBeCrowdfundNum() - 1;
            activity.setBeCrowdfundNum(beCrowdfundNum);

            activity.setSalesNum(activity.getSalesNum() + 1);

            Float salesAmount = BigDecimalUtils.add(activity.getSalesAmount(), payment);
            salesAmount = BigDecimalUtils.round(salesAmount, 2);
            activity.setSalesAmount(salesAmount);
        }
        activityService.update(activity);
        return activity;
    }

    /**
     * @param actualAmount 已筹金额
     * @param targetAmount 目标金额
     * @return
     * @Author yifeng
     * @Description 计算百分比
     * @Date 15:30 2019/4/17
     **/
    private int getPercent(Float actualAmount, Float targetAmount) {
        //支持前百分比
        double percent = BigDecimalUtils.div(actualAmount, targetAmount, 2);
        int intPercent = BigDecimalUtils.amplify2int(percent, 1);
        return intPercent;
    }

    /**
     * @param projectId 众筹id
     * @param orderForm 支持订单
     * @return
     * @Author yifeng
     * @Description 处理分销的数据
     * @Date 17:47 2019/4/11
     **/
    public void doStatistics(String projectId, OrderForm orderForm, boolean isSuccess,
                             Activity activity, Member crowdfundMember) {
        /*new Thread(() -> {*/
        try {
            DistributorRelation disRelation = distributorBizService.getDistributorRelation(projectId);
            SupportStatisticsInput supportStatisticsInput = new SupportStatisticsInput();
            supportStatisticsInput.setProjectId(projectId);
            supportStatisticsInput.setSupportId(orderForm.getId());
            supportStatisticsInput.setPayment(orderForm.getPayment());
            supportBizService.statistics(supportStatisticsInput, disRelation, isSuccess, o -> {
                // 处理合伙人的统计数
                updateCopartnerDetail(projectId, orderForm.getPayment(), isSuccess, activity,
                        crowdfundMember, disRelation);
                return null;
            });
        } catch (Exception e) {
            logger.error("支持数据统计分销数据异常", e);
        }
        /*}).start();*/
    }

    /**
     * @param activity  活动
     * @param orderForm 订单
     * @param isSuccess 众筹状态
     * @return
     * @Author yifeng
     * @Description 没有成功的后续 合作商数据统计 合伙人数据统计
     * @Date 16:54 2019/4/18
     **/
    public void doNotSuccessOpt(Activity activity, OrderForm orderForm, boolean isSuccess) {
        // 处理合作商的统计数
        updateMerchantDetail(activity, orderForm.getPayment(), isSuccess);
    }

    /**
     * @param orderForm       支持订单
     * @param crowdfundMember 众筹者
     * @param orgMember       发布者
     * @param project         众筹
     * @param activity        项目信息
     * @param mainOrderId     主订单id
     * @return
     * @Author yifeng
     * @Description 众筹成功的后续 消息推送 合作商统计数处理
     * @Date 17:34 2019/4/11
     **/
    public void doSuccessOpt(OrderForm orderForm, Member crowdfundMember, MemberGroup orgMember, Project project,
                             Activity activity, String mainOrderId, boolean isSuccess) {

        /*new Thread(() -> {*/
        // 消息推送
        long start13 = System.currentTimeMillis();
        sendCrowdfundSuccessMessage(crowdfundMember, orgMember, project.getId(), activity, mainOrderId);
        long end13 = System.currentTimeMillis();
        logger.info("消息推送：{}(ms)",(end13 - start13));
        // 海报推送
        new Thread(() -> {
            try {
                long start12 = System.currentTimeMillis();
                sendCrowdfundSuccessPoster(project, activity.getMember(), crowdfundMember, orgMember, activity);
                long end12 = System.currentTimeMillis();
                logger.info("单场画海报推送用时：{}(ms)",(end12 - start12));
            } catch (Exception e) {
                logger.error("单场画海报推送用时异常", e);
            }
        }).start();
        // 处理合作商的统计数
        long start14 = System.currentTimeMillis();
        updateMerchantDetail(activity, orderForm.getPayment(), isSuccess);
        long end14 = System.currentTimeMillis();
        logger.info("处理合作商的统计数：{}(ms)",(end14 - start14));
        /*}).start();*/
    }

    /**
     * @param activity  项目
     * @param payment   支持金额
     * @param isSuccess 成功状态
     * @return
     * @Author yifeng
     * @Description 更新合作商统计数
     * @Date 17:40 2019/4/11
     **/
    public void updateMerchantDetail(Activity activity, Float payment, boolean isSuccess) {
        /*new Thread(() -> {*/
        if (isSuccess) {
            try {
                memberMerchantDetailBizService.updateCrowdfundSuccess(activity.getMember(), payment);
            } catch (Exception e) {
                logger.error("实时更新众筹成功后机构统计数异常", e);
            }
        } else {
            try {
                memberMerchantDetailBizService.updateOrderNum(activity.getMember(), payment);
            } catch (Exception e) {
                logger.error("实时更新支持后机构统计数异常", e);
            }
        }
        /*}).start();*/
    }

    /**
     * @param projectId       众筹id
     * @param payment         金额
     * @param isSuccess       是否成功
     * @param activity        众筹项目
     * @param crowdfundMember 众筹者
     * @param disRelation     分销关系
     * @return
     * @Author yifeng
     * @Description 更新合伙人数据统计
     * @Date 16:51 2019/4/18
     **/
    public void updateCopartnerDetail(String projectId, Float payment, boolean isSuccess, Activity activity,
                                      Member crowdfundMember, DistributorRelation disRelation) {
        if (null != disRelation && distributorBizService.isCrowOrCourse(disRelation.getType())) {
            if (isSuccess) {
                try {
                    memberCopartnerDetailBizService.updateCrowdfundSuccess(disRelation, payment);
                } catch (Exception e) {
                    logger.error("实时更新众筹成功后合伙人统计数异常", e);
                }
                try {
                    brokerageBizService.insertBrokerageItem(disRelation, projectId, activity,
                            BrokerageItem.TYPE_CROWDFUND, crowdfundMember);
                } catch (Exception e) {
                    logger.error("插入分销佣金记录异常", e);
                }
            } else {
                try {
                    memberCopartnerDetailBizService.updateOrderAnalyze(disRelation, payment);
                } catch (Exception e) {
                    logger.error("实时更新众筹成功后合伙人统计数异常", e);
                }
            }
        }
    }

    /**
     * @param project 众筹信息
     * @param orgId   发布者id
     * @return
     * @Author yifeng
     * @Description 众筹成功后推送海报
     * @Date 17:26 2019/4/11
     **/
    public void sendCrowdfundSuccessPoster(Project project, String orgId, Member crowdfundMember, MemberGroup orgMember, Activity activity) {
        try {
            final String projectId = project.getId();
            final String orderMemberId = project.getAuthorId();
            if (project.getPayType() == 0) {
                // 在微信公众号上给众筹者推送成功海报
                final String activityMemberId = orgId;
                /*new Thread(() -> {*/
                messageOrderBizService.raiseSuccessfulPostersSend(projectId, orderMemberId, activityMemberId,
                        crowdfundMember, orgMember, activity);
                /*}).start();*/
            }
        } catch (Exception e) {
            logger.error("推送成功海报失败", e);
        }
    }

    /**
     * @param crowdfundMember 众筹者
     * @param orgMember       发布者者
     * @param projectId       众筹id
     * @param activity        项目信息
     * @return
     * @Author yifeng
     * @Description 众筹成功推送
     * @Date 17:09 2019/4/11
     **/
    public void sendCrowdfundSuccessMessage(Member crowdfundMember, MemberGroup orgMember, String projectId, Activity activity, String mainOrderId) {
        OrderForm orderForm = orderFormService.get(mainOrderId);
        MemberAct memberAct = memberActService.findByOrderId(mainOrderId);
        List<String> verifyCodeList = payOrderBizService.getCouponsList(orderForm);
        for (String verifyCode : verifyCodeList) {
            try {
                notifySendService.sendCorowdfund(orderForm, verifyCode, crowdfundMember, orgMember);
            } catch (Exception e) {
                logger.error("众筹成功通知失败---通知众筹者", e);
            }
        }
        try {
            officialPartnerNotifySendService.sendCrowdfundSuccess(orderForm, crowdfundMember, orgMember, projectId);
        } catch (Exception e) {
            logger.error("众筹成功通知失败---通知合作商", e);
        }

        try {
            // 代言人 合伙人 众筹成功消息推送
            String distributorId = distributorBizService.getDistributorId(memberAct.getId());
            Integer distributionType = distributorBizService.getDistributorType(memberAct.getId());
            if (StringUtils.isNotEmpty(distributorId) && null != distributionType) {
                // 联合发起人推送
                Member partnerReceiver = memberService.get(distributorId);
                if (null != partnerReceiver) {
                    if (DistributorRelationType.CROWDFUND_REPRESENT.getCode().equals(distributionType)) {
                        copartnerNotifySendService.sendCrowdfundSuccess(orderForm, crowdfundMember, partnerReceiver.getId(),
                                partnerReceiver.getMobile(), IdentityType.PARTNER.getCode(), projectId);
                    } else if (DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode().equals(distributionType)) {
                        copartnerNotifySendService.sendCrowdfundSuccess(orderForm, crowdfundMember, partnerReceiver.getId(),
                                partnerReceiver.getMobile(), IdentityType.CHANNEL_DIS.getCode(), projectId);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("众筹成功通知失败---通知联合发起人", e);
        }

        try {
            // 合作渠道推送
            if (StringUtils.isNotEmpty(activity.getCosponsorId())) {
                MemberGroup cosponsor = memberGroupService.get(activity.getCosponsorId());
                if (null != cosponsor) {
                    copartnerNotifySendService.sendCrowdfundSuccess(orderForm, crowdfundMember, cosponsor.getId(),
                            cosponsor.getMobile(), IdentityType.COSPONSOR.getCode(), projectId);
                }
            }
        } catch (Exception e) {
            logger.error("众筹成功通知失败---通知合作渠道", e);
        }
    }
}
